package me.flyray.biz.thymeleaf.modules.wechat.api;

import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.wxiaoqi.security.common.msg.ResponseCode;
import com.github.wxiaoqi.security.common.qr.request.GetWechatUserParam;
import com.github.wxiaoqi.security.common.util.EntityUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.flyray.biz.thymeleaf.api.BaseController;
import me.flyray.biz.thymeleaf.modules.wechat.biz.WechatBiz;

@Api(tags="微信管理")
@Controller
@RequestMapping("wechat")
public class WechatController extends BaseController {
	
	@Autowired
	private WechatBiz wechatBiz;
	
	/**
	 * 获取微信用户信息
	 * @return
	 * @throws Exception 
	 */ 
	@ApiOperation("获取微信用户信息")
	@RequestMapping(value = "/getWechatUserInfo",method = RequestMethod.GET)
    public String getWechatUserInfo(@RequestParam(value = "code") String code, @RequestParam(value = "goodsId") String goodsId, 
    		@RequestParam(value = "goodsPic") String goodsPic, @RequestParam(value = "goodsPrice") String goodsPrice, 
    		@RequestParam(value = "platformId") String platformId, @RequestParam(value = "merchantId") String merchantId) throws Exception {
		Map<String, Object> requestMap = new HashMap<String, Object>();
		requestMap.put("code", code);
		requestMap.put("goodsId", goodsId);
		requestMap.put("goodsPic", goodsPic);
		requestMap.put("goodsPrice", goodsPrice);
		requestMap.put("platformId", platformId);
		requestMap.put("merchantId", merchantId);
		Map<String, Object> response = wechatBiz.getWechatUserInfo(requestMap);
		if(ResponseCode.OK.getCode().equals(response.get("code"))){
			return "redirect:/dopay?openid="+response.get("openid")+"&nickname="+(String)response.get("nickname")+"&goodsPrice="+
					response.get("goodsPrice")+"&goodsPic="+response.get("goodsPic")+"&goodsId="+response.get("goodsId")+
					"&platformId="+response.get("platformId")+"&merchantId="+response.get("merchantId");
		}else{
			return "redirect:/payFail";
		}
    }

}
